package com.ao.andou.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ao.andou.R;
import com.ao.andou.common.OauthHelper;

/**
 * 
 * @author Andrew Wang
 * @version 1.0.0
 */
public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";
    
    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        GridView grid = (GridView)findViewById(R.id.home);
        grid.setAdapter(new ImageAdapter(this));
        ImageView logo = (ImageView)findViewById(R.id.activity_logo);
        logo.setImageResource(R.drawable.icon);
        TextView title = (TextView)findViewById(R.id.activity_title);
        title.setText(R.string.home_title);
        
        // STEP 1 : Get the access_token from preference
        String token = OauthHelper.getAccessToken(getApplicationContext());
        
        if (TextUtils.isEmpty(token)) {
            // STEP 2 : No authorization
            Intent loginIntent = new Intent(getApplicationContext(), LoginActivity.class);
            loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            startActivity(loginIntent);
        }
    }

    /*
     * GridView data adapter
     */
    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.home_miniblog, R.drawable.home_friend,
                R.drawable.home_user, R.drawable.home_favorite
        };
    }

}
